<?php
	
class Database{
		
		public $conn;
		// connect and set option
		/**
		 * Database wrapper for connect
		 */
		public function _connect(){	
			$user = "system";
			$pwd = "leoeric";
			$dbname = "localhost/cs5226";
			$this->conn = oci_connect($user, $pwd, $dbname);
			
			/*
			$sql = oci_parse($this->conn, "select * from dbusers where name='system'");
			oci_execute($sql);
			while ($row = oci_fetch_assoc($sql)) {
				echo $row['NAME'].'-'.$row['PASS'];
			}*/
			
			return $this->conn;
		}
		public function connect(){
			if(!$this->_connect()){
				echo 'error,unable to connect to database';
			}
		}
		public function db_query($query){
			//echo $query;
			
			return oci_parse($this->conn, $query);
		}
		/**
		 * Database wrapper for close
		 */
		public function _close(){
			return oci_close($this->conn);
		}
		/**
		 * Database wrapper for select
		 */
		public function select($table,$condition,$order=null){
			//var_dump($this->conn);
			$sql = "SELECT * FROM $table WHERE ";
			$temp = array();
			foreach($condition as $key=>$val){
				$temp[] = "$key='$val'";
			}
			$con = implode(' AND ', $temp);
			$sql = $sql.$con.$order;
			//echo $sql;
			$result = $this->db_query($sql);
			oci_execute($result);
			return $result;
		}
		
		public function execute($res) {
			oci_execute($res);
		}
		/**
		 * Database wrapper for remove
		 */
		public function remove($table,$condition){
			$sql = "DELETE FROM $table WHERE ";
			$temp = array();
			foreach($condition as $key=>$val){
				$temp[] = "$key='$val'";
			}
			$con = implode(' AND ', $temp);
			$sql = $sql.$con;
			$result = $this->db_query($sql);
			oci_execute($result);
			return $result;
		}
		/**
		 * Database wrapper for insert
		 */
		public function insert($table,$value){
			$sql = "INSERT INTO $table ";
			$temp_field = array();
			$temp_val = array();
			foreach($value as $key=>$val){
				$temp_field[] = $key;
				$temp_val[] = "'$val'";
			}
			$field = implode(',', $temp_field);
			$val = implode(',', $temp_val);
			$sql = $sql."($field) VALUES ($val)";
			//echo $sql;
			$result = $this->db_query($sql);
			//var_dump($sql);
			oci_execute($result);
			return $result;
		}
		/**
		 * Database wrapper for update
		 */
		 
		public function update($table,$value,$condition){
			$sql = "UPDATE $table SET ";
			$temp_field = array();
			$temp_val = array();
			$temp = array();
			foreach($condition as $key=>$val){
				$temp[] = "$key='$val'";
			}
			$con = implode(' AND ', $temp);
			foreach($value as $key=>$val){
				$temp_val[] = "$key='$val'";
			}
			$update = implode(',', $temp_val);
			$sql = $sql.$update." WHERE ".$con;
			//echo $sql;
			$result = $this->db_query($sql);
			oci_execute($result);
			return $result;
		}
		/**
		 * Database wrapper for fetch_array()
		 */
		 
		public function db_fetch_array($result){
			return oci_fetch_assoc($result);
		} 
	}


?>


